package com.ctrip.ec.dal;



import java.sql.SQLException;

/**
 * Created by jiang on 2018/3/22.
 */
public class PagerSetting {

    private int pageSize;
    private int recordCount;
    private int pageIndex;
    private String orderBy;
    private String where;
    private String select;
    private long skipCount;

    public PagerSetting() {
        this.pageSize = Integer.MAX_VALUE;
        this.pageIndex = 1;
    }

    public int getPageSize() {
        return pageSize;
    }

    public PagerSetting setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }


    public int getRecordCount() {
        return recordCount;
    }

    public PagerSetting setRecordCount(int val) {
        this.recordCount = val;
        return this;
    }


    public int getPageIndex() {
        return pageIndex;
    }

    public PagerSetting setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
        return this;
    }

    public long getSkipCount() {
        return (pageIndex - 1) * pageSize;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public PagerSetting setOrderBy(String orderBy) {
        this.orderBy = orderBy;
        return this;
    }

    public String getWhere() {
        return where;
    }

    public PagerSetting setWhere(String where) {
        this.where = where;
        return this;
    }

    public  PagerSetting appendWhere(String where){
        if(this.where == null || this.where.length()==0){
            this.where = where;
        }else{
            this.where += " and " + where;
        }
        return this;
    }

    public PagerSetting setSkipCount(long skipCount) {
        this.skipCount = skipCount;
        return this;
    }

    public String getSelect() {
        return select;
    }

    public void setSelect(String select) {
        this.select = select;
    }

    public String buildPagerSql(DaoBase dao, Object... paras) throws SQLException {

        String currentSelect = "select * from " + dao.getTableName();
        if(this.select !=null && this.select.length()>0){
            currentSelect= this.select;
        }

        String where = "";

        if (this.where != null && this.where.length() > 0) {
            where = " where " + this.getWhere();
        }

        String orderBy = "order by " + dao.getPkName() + " desc";

        if (this.getOrderBy() != null && this.getOrderBy().length() > 0) {
            orderBy = "order by " + this.getOrderBy();
        }

        this.recordCount = dao.getDb().getScalar(Integer.class, "select count(*) from (" + currentSelect + ") as t " + where, paras);

        String sql = null;

        if (this.pageSize == Integer.MAX_VALUE) {
            sql = "select * from (" + currentSelect + ") as t " + where + " " + orderBy;

        } else {

            int pageCount = this.recordCount / this.pageSize;//总页数
            if (pageCount < 1) {
                pageCount = 1;
            }

            if (this.recordCount % this.pageSize > 0) {
                pageCount++;
            }
            if (this.pageSize < 1 || this.pageIndex < 1) {
                this.pageIndex = 1;
            }

            int begin = (this.pageIndex - 1) * this.pageSize;

            if (dao.db.getProvider() == DbProvider.MYSQL) {

                sql = "select * from ("  + currentSelect + ") as t " + where + " " + orderBy + " limit " + begin + "," + this.getPageSize();
            } else if (dao.db.getProvider() == DbProvider.MSSQL) {
                begin++;
                long end = begin + this.getPageSize() - 1;
                sql = "select * from (select (row_number() over (" + orderBy
                        + ")) as RowID, * from (" + currentSelect + ") as b where " +
                        where + ") as a where RowID between " + begin + " and " + end;

            }

        }

        return sql;
    }
}
